<form id="dictFieldItemForm" class="form-horizontal"  method="post">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h5 class="blue">新增字段</h5>
    </div>

    <div class="modal-body">
        <input type="hidden" name="id" id="id" value="${dictFieldItem.id!}">
        <input type="hidden" name="fieldId" id="fieldId" value="${dictFieldItem.fieldId!}">
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="itemKey">字段名:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="clearfix">
                    <input type="text" name="itemKey" id="itemKey" class="col-xs-12 col-sm-4 required" value="${dictFieldItem.itemKey!}"/>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="itemValue">字段值:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="clearfix">
                    <input type="text" name="itemValue" id="itemValue" class="col-xs-12 col-sm-4 required" value="${dictFieldItem.itemValue!}"<#if dictFieldItem.id??>  readonly</#if>/>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="itemTitle">字段标题:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="clearfix">
                    <input type="text" name="itemTitle" id="itemTitle" class="col-xs-12 col-sm-4 required" value="${dictFieldItem.itemTitle!}"/>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="sortIndex">字段排序:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="clearfix">
                    <input type="text" name="sortIndex" id="sortIndex" class="col-xs-12 col-sm-4 required" value="${dictFieldItem.sortIndex!}"/>
                </div>
            </div>
        </div>

    <div class="modal-footer center">
        <button type="submit" class="btn btn-sm btn-success"><i class="ace-icon fa fa-check"></i> 保存</button>
        <button type="button" class="btn btn-sm" data-dismiss="modal"><i class="ace-icon fa fa-times"></i> 取消</button>
    </div>

</form>
<script src="${ctx}/static/admin/assets/js/jquery.validate.js"></script>
<script src="${ctx}/static/admin/assets/js/jquery.validate.messages_zh.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $(".date-picker").datepicker({
            language : 'zh-CN',
            autoclose: true,
            todayHighlight: true
        });

        $("#dictFieldItemForm").validate({
            rules:{itemKey:{required:true,
                            remote:{
                                    url: "/admin/system/dictFieldItem/check.do", //后台处理程序
                                    type: "get",  //数据发送方式
                                    data:  {                     //要传递的数据
                                            id: function() { return $("#id").val();},
                                            fieldId: function() { return $("#fieldId").val();},
                                            itemKey: function() { return $("#itemKey").val();}
                                          }
                                   }
                        },
                itemValue:{required:true,
                    remote:{
                        url: "/admin/system/dictFieldItem/check.do", //后台处理程序
                        type: "get",  //数据发送方式
                        data:  {                     //要传递的数据
                            id: function() { return $("#id").val();},
                            fieldId: function() { return $("#fieldId").val();},
                            itemValue: function() { return $("#itemValue").val();}
                        }
                    }
                },
                sortIndex:{required:true,
                           number:true },
                itemTitle:{required:true,
                    remote:{
                        url: "/admin/system/dictFieldItem/check.do", //后台处理程序
                        type: "get",  //数据发送方式
                        data:  {                     //要传递的数据
                            id: function() { return $("#id").val();},
                            fieldId: function() { return $("#fieldId").val();},
                            itemTitle: function() { return $("#itemTitle").val();}
                        }
                    }
                }
            },
            messages:{ itemKey:{ required:"请填字段名",remote:"该字段名已存在"},
                       itemValue:{ required:"请填字段值", remote:"该字段值已存在"},
                       itemTitle:{ required:"请填字段中文名",remote:"该字段中文名已存在" },
                       sortIndex:{ required:"请填字段数字排序值",number:"该字段只能填数字" }
                      },
            errorElement: 'div',
            errorClass: 'help-block',
            focusInvalid: true,
            highlight: function (e) {
                $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
            },
            success: function (e) {
                $(e).closest('.form-group').removeClass('has-error');//.addClass('has-info');
                $(e).remove();
            },
            errorPlacement: function (error, element) {
                if(element.is('input[type=checkbox]') || element.is('input[type=radio]')) {
                    var controls = element.closest('div[class*="col-"]');
                    if(controls.find(':checkbox,:radio').length > 1) controls.append(error);
                    else error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
                }
                else if(element.is('.select2')) {
                    error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
                }
                else if(element.is('.chosen-select')) {
                    error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
                }
                else error.insertAfter(element.parent());
            },

            submitHandler: function (form) {
                $.ajax({
                    type: "POST",
                    url:"${ctx}/admin/system/dictFieldItem/save.do",
                    data:$("#dictFieldItemForm").serialize(),
                    dataType: 'text',
                    success: function(msg){
                        var msg = eval("("+msg+")");
                        if(msg.result == "success"){
                            window.location="${refer!}";
                        }else{
                            alert(msg.messages);
                            //bootbox.alert(msg.messages);
                        }
                    },
                    error: function(){
                        bootbox.alert("保存失败");
                    }
                });
            },
            invalidHandler: function (form) {
            }
        })

    });
</script>